package jmine.tec.script.impl;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class AScriptParametersImplShould {

    private ScriptParametersImpl params;

    @Before
    public void before() {
        this.params = new ScriptParametersImpl();
    }

    @Test
    public void addAParameter() {
        Assert.assertTrue(this.params.getParameterValues().isEmpty());
        Assert.assertTrue(this.params.getParameterDefinitions().isEmpty());

        Object value = new Object();
        String key = "x";

        this.params.putParameter(key, value);

        Assert.assertFalse(this.params.getParameterValues().isEmpty());
        Assert.assertFalse(this.params.getParameterDefinitions().isEmpty());
        Assert.assertSame(value, this.params.getParameterDefinition(key).getHead());
    }

    @Test
    public void addANullParameter() {
        Assert.assertTrue(this.params.getParameterValues().isEmpty());
        Assert.assertTrue(this.params.getParameterDefinitions().isEmpty());

        Object value = null;
        String key = "x";

        this.params.putParameter(key, value);

        Assert.assertFalse(this.params.getParameterValues().isEmpty());
        Assert.assertFalse(this.params.getParameterDefinitions().isEmpty());
        Assert.assertSame(value, this.params.getParameterDefinition(key).getHead());
    }

    @Test
    public void addAParameterWithClass() {
        Assert.assertTrue(this.params.getParameterValues().isEmpty());
        Assert.assertTrue(this.params.getParameterDefinitions().isEmpty());

        Object value = new Object();
        String key = "x";

        this.params.putParameter(key, value, String.class);

        Assert.assertFalse(this.params.getParameterValues().isEmpty());
        Assert.assertFalse(this.params.getParameterDefinitions().isEmpty());
        Assert.assertSame(value, this.params.getParameterDefinition(key).getHead());
        Assert.assertSame(String.class, this.params.getParameterDefinition(key).getTail());
    }

    @Test
    public void defineAParameter() {
        Assert.assertTrue(this.params.getParameterValues().isEmpty());

        String key = "x";

        this.params.defineParameter(key, String.class);

        Assert.assertFalse(this.params.getParameterValues().isEmpty());
        Assert.assertNull(this.params.getParameterDefinition(key).getHead());
        Assert.assertSame(String.class, this.params.getParameterDefinition(key).getTail());
    }

    @Test
    public void redefineAParameter() {
        this.defineAParameter();

        String key = "x";

        this.params.defineParameter(key, Integer.class);

        Assert.assertFalse(this.params.getParameterValues().isEmpty());
        Assert.assertNull(this.params.getParameterDefinition(key).getHead());
        Assert.assertSame(Integer.class, this.params.getParameterDefinition(key).getTail());
    }

    @Test
    public void putConstants() {
        Assert.assertTrue(this.params.getConstants().isEmpty());

        this.params.putConstant("bool", true);
        this.params.putConstant("byte", (byte) 100);
        this.params.putConstant("char", 'c');
        this.params.putConstant("double", 1D);
        this.params.putConstant("enum", MyEnum.BLA);
        this.params.putConstant("float", 1F);
        this.params.putConstant("int", 1);
        this.params.putConstant("long", 1L);
        this.params.putConstant("short", (short) 1);
        this.params.putConstant("string", "string");

        Assert.assertFalse(this.params.getConstants().isEmpty());

        Assert.assertEquals(this.params.getConstants().get("bool"), true);
        Assert.assertEquals(this.params.getConstants().get("byte"), (byte) 100);
        Assert.assertEquals(this.params.getConstants().get("char"), 'c');
        Assert.assertEquals(this.params.getConstants().get("double"), 1D);
        Assert.assertEquals(this.params.getConstants().get("enum"), MyEnum.BLA);
        Assert.assertEquals(this.params.getConstants().get("float"), 1F);
        Assert.assertEquals(this.params.getConstants().get("int"), 1);
        Assert.assertEquals(this.params.getConstants().get("long"), 1L);
        Assert.assertEquals(this.params.getConstants().get("short"), (short) 1);
        Assert.assertEquals(this.params.getConstants().get("string"), "string");

    }

    private static enum MyEnum {
        BLA
    }

}
